package ocr;

import java.io.File;

import net.sourceforge.tess4j.ITesseract;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
import net.sourceforge.tess4j.util.LoadLibs;

public class Tess4jOcr {

	public static void main(String[] args) throws TesseractException {
		/*ITesseract instance = new Tesseract();
        instance.setDatapath("E:\testdata");
        File imgDir = new File("");
        //对img_data文件夹中的每个验证码进行识别
        //文件名即正确的结果
        for (File imgFile : imgDir.listFiles()) {
            //该例子输入的是文件，也可输入BufferedImage           
            String ocrResult = instance.doOCR(imgFile);
            //输出图片文件名，即正确识别结果
            System.out.println("ImgFile: "+imgFile.getAbsolutePath());
            //输出识别结果
            System.out.println("OCR Result: " + ocrResult);
        }*/
		/*ITesseract instance = new Tesseract();
        //如果未将tessdata放在根目录下需要指定绝对路径
        instance.setDatapath("E:\\testdata");
        // 我们需要指定识别语种
//        instance.setLanguage("eng");
        // 指定识别图片
        File imgDir = new File("E:\\testdata\\003bsgbmgy6R6efoOr1c3.png");
        long startTime = System.currentTimeMillis();
        String ocrResult = instance.doOCR(imgDir);
        // 输出识别结果
        System.out.println("OCR Result: \n" + ocrResult + "\n 耗时：" + (System.currentTimeMillis() - startTime) + "ms");*/
		
//		System.setProperty("jna.library.path", "32".equals(System.getProperty("sun.arch.data.model")) ? "lib/win32-x86" : "lib/win32-x86-64");
        File imageFile = new File("E:\\testdata\\yanzhengma.jpg");
        ITesseract instance = new Tesseract();  // JNA Interface Mapping
//        instance.setDatapath("E:\\testdata\\tessdata");
       
         File tessDataFolder = LoadLibs.extractTessResources("tessdata"); // Maven build bundles English data
         System.out.println(tessDataFolder.getParent());
         instance.setDatapath(tessDataFolder.getParent());

        try {
            long start = System.currentTimeMillis();
            String result = instance.doOCR(imageFile);
            System.out.println("花费时间："+ (System.currentTimeMillis()-start) +"毫秒");
            System.out.println(result);
        } catch (TesseractException e) {
            System.err.println(e.getMessage());
        }
	}

}
